package com.goldcn.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.goldcn.interceptor.CommonInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * Created by Administrator on 2016/3/4.
 */
@Configuration

public class WebAppConfig extends WebMvcConfigurationSupport {


    //WebMvcConfigurerAdapter 过时

    //spring封装httpcilent的类，带有连接池
/*    @Bean(name = "restTemplate")
    @LoadBalanced
    public RestTemplate restTemplate() {
        ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        return new RestTemplate(clientHttpRequestFactory);
    }*/

    /**
     * 不通过服务名访问
     *
     * @return
     */
    @Bean(name = "rest")
    public RestTemplate rest() {
        ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        return new RestTemplate(clientHttpRequestFactory);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
    }

    //=====================zll ==============
    @Bean(name = "OSSConfigure")
    public OSSConfigure oSSConfigure() {
        return new OSSConfigure();
    }

    @Bean(name = "BankbaseCfg")
    public BankbaseCfg bankbaseCfg() {
        return new BankbaseCfg();
    }


    @Bean(name = "ErpInterfaceCfg")
    public ErpInterfaceCfg erpInterfaceCfg() {
        return new ErpInterfaceCfg();
    }

    @Bean(name = "ErpBorrowInterfaceCfg")
    public ErpBorrowInterfaceCfg ErpBorrowInterfaceCfg() {
        return new ErpBorrowInterfaceCfg();
    }

    //配置拦截器拦截的URL
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CommonInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}
